<?php

namespace Home\Controller;
use Think\Controller;
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');  
// 响应类型  
header('Access-Control-Allow-Methods:*');  
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type');  
class OrderController extends Controller
{
 
     public function luxian()
     {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $data=D('price')->field('id,from_city,end_city,price,ck_city')->select();
        if(empty($data))
        {
            datamsg('1',"暂无更多",$data);
        }
        datamsg('1',"成功",$data);
     }     

     //报单  
     public function confirm()
    {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $paramdata=I("post.");
        $user=user_cinfo($paramdata['user_id'],$paramdata['token']);
        $driver = M('siji')->where(array('openid'=>$user['openid']))->field('xingming,dianhua,city')->find();
        if(empty($paramdata['pathid'])){
            datamsg(0,'线路不能为空');
        }
        if(empty($paramdata['count'])){
            datamsg(0,'乘车人数不能为空');
        }
        if(empty($paramdata['chufa_time'])){
            datamsg(0,'乘车时间不能为空');
        }
        if(empty($paramdata['guding'])){
            datamsg(0,'固定乘车点不能为空');
        }
        if(empty($paramdata['mobile'])){
            datamsg(0,'电话号码不能为空');
        }else{
            foreach($paramdata['mobile'] as $value){
                if(!preg_match("/^1[123456789]{1}\d{9}$/",$value)){
                    datamsg(0,'电话号码格式不正确');
                }
            }
        }

        if(I('pathid')!='')
        {
            $path=D("price")->where("id=".$paramdata['pathid'])->find();
        }else{
            datamsg(0,'线路不能为空');
        }
        //$orderid = "H666" . "-" . date('YmdHis') . rand(10000, 99999);
        $orderid = unqiueNumber();
        $data['openid'] = $user['openid'];
        $data['start'] = $path['from_city'];
        $data['end'] = $path['end_city'];
        $data['price'] =$path['price'];
        $data['starttime'] = $paramdata['chufa_time'];
        $data['addtime'] = time();
        $data['num'] = 1;
        $data['orderid'] = $orderid;
//        $data['start_x'] = I('start_x');
//        $data['end_x'] = I('end_x');
        // $data['mobile'] = implode(',',$paramdata['mobile']);
        $data['pathid'] = $paramdata['pathid'];
        $data['guding'] = $paramdata['guding'];
        $data['dianhua'] = $driver['dianhua'];
        $data['xingming']=$driver['xingming'];

        if(empty($data['start']) || empty($data['end']) || empty($data['price']) || empty($data['starttime']) || empty($data['num']))
        {
            $data['order_cwstatus']='2';
        }else
        {
            $data['order_cwstatus']='1';
        }

        // var_dump($data);die;

        $i=0;
   
        for($i=0;$i<$paramdata['count'];$i++)
        {   
            $data['mobile'] = $paramdata['mobile'][$i];
            $info['id'] = D('xllog')->add($data);
        }
        
        $info['order_id']=$orderid;
        $info['from_city']=$path['from_city'];
        $info['end_city']=$path['end_city'];
        $info['price']=$path['price'];
        if(empty($info))
        {
            datamsg('0',"系统繁忙，请稍后再试");
        }

        datamsg('1',"报单成功",$info);
    }


    //0: 创建订单
    //1：发车
    //2: 到达固定乘车点
    //3：接到乘客
    //4：到达终点站
    //5：确认订单
    //6：订单完成
    public function order_status()
    {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $user_id=I("user_id");
        $token=I("token");
        $order_id=I("id");
        $status['status']=I("status");
        $rules = array(
             array('id','require','参数错误',1),
             array('status','require','参数错误',1),

        );

        $wxuser = M("xllog"); 
        if (!$wxuser->validate($rules)->create()){
            datamsg(0,$wxuser->getError());
        }

        $user=user_cinfo($user_id,$token);


        $order =D('xllog')->field('id,status,price')->where('id='.$order_id)->find();
        

        if(empty($order))
        {
            datamsg(0,'暂无该订单');
        }
        if($order['status']==$status['status'])
        {
            datamsg(0,'请不要重复操作');
        }
       
        $order1 =M('xllog')->where('id='.$order_id)->save($status);
        if(empty($order1))
        {
            datamsg(0,"系统繁忙，请稍后再试");
        }

        if($status['status']>='4')
        {
            datamsg('1','操作成功',$order['price']);
        }
        datamsg('1','操作成功');
    }


    /*
     * 提现列表
     */
    public function withdrawlist(){
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $uid=I("post.user_id");
        if(empty($uid)){
            datamsg(0,'请输入id');
        }
        $token=I("post.token");
        if(empty($token)){
            datamsg(0,'请输入token');
        }
        user_cinfo($uid,$token);
        $type = I('post.type');
        if(empty($type)){
            datamsg(0,'请输入类型');
        }
        switch ($type){
            case 1://全部
                $openid = M('wxuser')->where(array('uid'=>$uid))->getField('openid');
                $where['openid']=$openid;
                $where['status']=array('egt',6);
                $result1 = M('xllog')->where($where)->field('orderid as card,price as money,siji as create_time')->select();

                foreach ($result1 as $key => $value) {
                    $result1[$key]['desc'] = "订单号";
                }

                $result2 = M('withdraw')->where(array('uid'=>$uid))->field('money,status,create_time,card')->select();
                foreach ($result2 as $key => $value) {
                    $result2[$key]['desc'] = "卡号";
                }
                $result= array_merge($result1,$result2);
             
                foreach($result as $key=>&$value){
                    $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                }
                break;
            case 2://支出
                $result = M('withdraw')->where(array('uid'=>$uid,'status'=>2))->field('money,status,create_time,card')->select();
                foreach($result as $key=>&$value){
                    $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                }
                break;
            case 3://收入
                $openid = M('wxuser')->where(array('uid'=>$uid))->getField('openid');
                $where['openid']=$openid;
                $where['status']=array('egt',6);
                $result = M('xllog')->where($where)->field('orderid,total_price,siji')->select();
                foreach($result as $key=>&$value){
                    $value['siji']=date('Y-m-d H:i:s',$value['siji']);
                }
                break;
            default:
                datamsg(0,'参数类型错误');
                break;
        }
        datamsg(1,"获取成功",$result);

    }

    /**
     * 提现
     */
    public function withdraw(){
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $data['uid']=I("post.user_id");
        if(empty($data['uid'])){
            datamsg(0,'请输入id');
        }
        $token=I("post.token");
        if(empty($token)){
            datamsg(0,'请输入token');
        }
        user_cinfo($data['uid'],$token);
        $data['money'] = I('post.money');
        if(empty($data['money'])){
            datamsg(0,'请输入提现金额');
        }else{
            if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $data['money'])) {
                datamsg(0,'价格错误');
            }
            $yue = M('wxuser')->where(array('uid'=>$data['uid']))->getField('yue');
            if($yue < $data['money']){
                datamsg(0,'余额不足');
            }
        }
        $data['card'] = I('post.card');
        if(empty($data['card'])){
            datamsg(0,'请输入提现卡号');
        }
        $data['name'] = I('post.name');
        if(empty($data['name'])){
            datamsg(0,'请输入姓名');
        }
        $data['create_time']=time();
        $data['status']=1;//提现
        $result = M('withdraw')->add($data);
        if($result){
            $yue= $yue-$data['money'];
            M('wxuser')->where(array('uid'=>$data['uid']))->save(array('yue'=>$yue));
            datamsg(1,'请求成功，等待后台审核');
        }else{
            datamsg(0,'网络错误，稍后重试');
        }


    }
    
}
